#include "game.h"

TestGame::TestGame() {

}

TestGame::~TestGame() { 

}
bool TestGame::OnInit() {

	_clearScreen = true;

	// ROY

	if(!roy.LoadIni("data/graphics/roy.spr"))
		return false;

	royWalk = new Animation();
	if(!royWalk)
		return false;

	royIdle = new Animation();
	if(!royIdle)
		return false;

	royDist = new Animation();
	if(!royDist)
		return false;

	if(!royWalk->Load("data/graphics/walk.ani"))
		return false;
	
	if(!royIdle->Load("data/graphics/idle.ani"))
		return false;

	if(!royDist->Load("data/graphics/distance.ani"))
		return false;

		
		roy.SetFrame(0);
        roy.SetPosY(100);

	
	royFlipped = false;

 
	// V

	if(!var.LoadIni("data/graphics/var.spr"))
		return false;

	varWalk = new Animation();
	if(!varWalk)
		return false;

	varIdle = new Animation();
	if(!varIdle)
		return false;

	varSleep = new Animation();
	if(!varSleep)
		return false;

	varFire = new Animation();
	if(!varFire)
		return false;

	if(!varWalk->Load("data/graphics/varWalk.ani"))
		return false;
	
	if(!varIdle->Load("data/graphics/varIdle.ani"))
		return false;

	if(!varSleep->Load("data/graphics/varSleep.ani"))
		return false;

	if(!varFire->Load("data/graphics/varFire.ani"))
		return false;

	var.SetFrame(0);
	var.SetPosY(-100);

	
	varFlipped = false;


	return true;
}

void TestGame::OnFrame() {
	float dt = (float)_fpsMeter.GetDT();

	if (KeyDown(DIK_ESCAPE))
		PostQuitMessage(0);

	//ROY
	
	if ((roy.GetAnimation()==royWalk) || (roy.GetAnimation()!=royWalk && !roy.IsAnimationRunning()) ){
		if (KeyPressed(DIK_RIGHT)){
			if (royFlipped){
				roy.FlipHorizontal();
				royFlipped=false;
			}
			roy.SetPosX(roy.GetPosX()+0.3f*dt);
			if(!roy.IsAnimationRunning())
				roy.SetAnimation(royWalk);
		}

		if (KeyPressed(DIK_LEFT)){
			if (!royFlipped){
				roy.FlipHorizontal();
				royFlipped=true;
			}
			roy.SetPosX(roy.GetPosX()-0.3f*dt);
			if(!roy.IsAnimationRunning())
				roy.SetAnimation(royWalk);
		}

		if (KeyUp(DIK_RIGHT) || KeyUp(DIK_LEFT))
			roy.SetAnimation(royIdle);

		if (KeyPressed(DIK_RIGHT) && KeyPressed(DIK_LEFT))
			roy.SetAnimation(royIdle);

 	// V

			if (KeyPressed(DIK_RIGHT)){
			if (varFlipped){
				var.FlipHorizontal();
				varFlipped=false;
			}
			var.SetPosX(var.GetPosX()+0.3f*dt);
			if(!var.IsAnimationRunning())
				var.SetAnimation(varWalk);
		}


		if (KeyPressed(DIK_LEFT)){
			if (!varFlipped){
				var.FlipHorizontal();
				varFlipped=true;
			}
			var.SetPosX(var.GetPosX()-0.3f*dt);
			if(!var.IsAnimationRunning())
				var.SetAnimation(varWalk);
		}

		if (KeyUp(DIK_RIGHT) || KeyUp(DIK_LEFT))
			var.SetAnimation(varIdle);

		if (KeyPressed(DIK_RIGHT) && KeyPressed(DIK_LEFT))
			var.SetAnimation(varIdle);

		
//Roy

		if (KeyDown(DIK_RIGHT) || KeyDown(DIK_LEFT))
			roy.SetAnimation(royWalk);

		if (KeyUp(DIK_RIGHT) && !KeyPressed(DIK_LEFT))
			roy.SetAnimation(royIdle);

		if (!KeyPressed(DIK_RIGHT) && KeyUp(DIK_LEFT))
			roy.SetAnimation(royIdle);

		if (KeyDown(DIK_RIGHT) && KeyPressed(DIK_LEFT))
			roy.SetAnimation(royIdle);

		if (KeyDown(DIK_LEFT) && KeyPressed(DIK_RIGHT))
			roy.SetAnimation(royIdle);

		if (KeyUp(DIK_RIGHT) && KeyPressed(DIK_LEFT))
			roy.SetAnimation(royWalk);

		if (KeyPressed(DIK_RIGHT) && KeyUp(DIK_LEFT))
			roy.SetAnimation(royWalk); // Codigo para setear animacion caso por caso, 
//resuelto en el keypressed del movimiento mas la contemplacion del caso especial
//cuando presiono los dos direccionales a la ves

	if (KeyDown(DIK_UP))
		roy.SetAnimation(royDist);
	}

	//V

	if (KeyDown(DIK_RIGHT) || KeyDown(DIK_LEFT))
			var.SetAnimation(varWalk);

		if (KeyUp(DIK_RIGHT) && !KeyPressed(DIK_LEFT))
			var.SetAnimation(varIdle);

		if (!KeyPressed(DIK_RIGHT) && KeyUp(DIK_LEFT))
			var.SetAnimation(varIdle);

		if (KeyDown(DIK_RIGHT) && KeyPressed(DIK_LEFT))
			var.SetAnimation(varIdle);

		if (KeyDown(DIK_LEFT) && KeyPressed(DIK_RIGHT))
			var.SetAnimation(varIdle);

		if (KeyUp(DIK_RIGHT) && KeyPressed(DIK_LEFT))
			var.SetAnimation(varWalk);

		if (KeyPressed(DIK_RIGHT) && KeyUp(DIK_LEFT))
			var.SetAnimation(varWalk);

		if (KeyDown(DIK_UP))
		var.SetAnimation(varSleep);

		if (KeyDown(DIK_DOWN))
		var.SetAnimation(varFire);

		if (KeyDown(DIK_SPACE))
			var.LoadIni("data/graphics/var_drow.spr");



    roy.Update(dt);
	var.Update(dt);
}

void TestGame::OnDraw() {
	wstringstream ss;

	ss << ZAK_ENGINE_NAME << endl << "FPS: " << _fpsMeter.GetFps() << endl;
	g_renderer.SetFontColor(0xFFFF0000);
	g_renderer.DrawString(ss.str(),10,10,800-20,600-20,ZAK_TEXT_LEFT);

 	roy.Draw();
	var.Draw();
}

bool TestGame::OnShutdown() {
	
	//Borrando a Roy
	
	delete royWalk;
	royWalk = NULL;

	delete royIdle;
	royIdle = NULL;

	delete royDist;
	royDist = NULL;

	//Borrando a V

	delete varWalk;
	varWalk = NULL;

	delete varIdle;
	varIdle = NULL;

	delete varSleep;
	varSleep = NULL;

	delete varFire;
	varFire = NULL;

	return true;
}